using System;
using M0;
using UnityEngine;

public partial class ExcelHelp
    {
        [Excel(typeof(int))]
        public static object Int值(object rowdata)
        {
            if (rowdata is DBNull)
            {
                return 0;
            }
            else
            {
                int value = 0;
                if (int.TryParse(rowdata.ToString(), out value))
                {
                    return value;
                }
                else
                {
                    BDebug.Error($"与定义的字段类型不同{rowdata.GetType()}!=int");
                    return 0;
                }
            }
        }
        
        [Excel(typeof(string))]
        public static object String值(object rowdata)
        {
            if (rowdata is DBNull)
            {
                return "";
            }
            else
            {
                return rowdata.ToString();
            }
        }
        
        [Excel(typeof(float))]
        public static object Float值(object rowdata)
        {
            if (rowdata is DBNull)
            {
                return 0f;
            }
            else
            {
                float value = 0;
                if (float.TryParse(rowdata.ToString(), out value))
                {
                    return value;
                }
                else
                {
                    BDebug.Error($"与定义的字段类型不同{rowdata.GetType()}!=float");
                    return 0f;
                }
            }
        }
        
        [Excel("bool")]
        public static object Bool值(object rowdata)
        {
            if (rowdata is DBNull)
            {
                return false;
            }
            else
            {
                string s = rowdata.ToString().ToLower();
                if (s == "true")
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
        
        [Excel("JsonV3")]
        public static object Excel字段分析_vector3(object rowdata)
        {
            if (rowdata is DBNull)
            {
                return new JsonV3();
            }
            else
            {
                string s = rowdata.ToString();
                if (s.Contains("(") && s.Contains(")"))
                {
                    string info = s.Replace("(", "").Replace(")", "");
                    var infos = info.Split(',');
                    float x = float.Parse(infos[0]);
                    float y = float.Parse(infos[1]);
                    float z = float.Parse(infos[2]);

                    return new Vector3(x, y, z).ToV3();
                }
            }
        
            return new JsonV3();
        }
    }

